#!/usr/bin/env bash
#!/usr/bin/env sh << 精简系统

# install.sh [name] [path]
# $1 ${DATA_PATH} 安装目录
# $2 ${APP_NAME} 应用名称
clear

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "请使用 root 账户安装!"
    exit 1
fi

# 当前脚本目录
CurrentDir=$(cd $(dirname $0) && pwd)
cd ${CurrentDir}

# 加载核心脚本
. sh/main.sh
. sh/init.sh

# 获取操作系统名($DISTRO) 及安装命令($PM)
Get_Dist_Name
Get_Dist_Version
if [ "${DISTRO}" = "unknow" ]; then
    Echo_Red "暂不支持在当前操作系统下部署."
    exit 1
fi
Echo_Green "${DISTRO} ${DISTRO_Version}"

# 安装目录
if [ ! -z "$1" ]; then
  DATA_PATH=$1
else
  CACHE=/data
  read -p "安装目录(default=${CACHE}): " DATA_PATH
fi
if [ -z "${DATA_PATH}" ]; then
  DATA_PATH="${CACHE}"
fi

# 安装目录
if [ ! -z "$2" ]; then
  APP_NAME=$2
else
  CACHE=lnmp
  read -p "应用名称(default=${CACHE}): " APP_NAME
fi
if [ -z "${APP_NAME}" ]; then
  APP_NAME="${CACHE}"
fi

# AUTHOR
if [ -z ${AUTHOR} ]; then
  read -p "编译作者(default=xbear): " AUTHOR
else
  echo "AUTHOR=${AUTHOR}"
fi
if [ -z ${AUTHOR} ]; then
  AUTHOR=xbear
fi

# 初始化安装配置
# -----------------------------------------------------
# 开发环境
# - ln -s /media/psf/Home/Documents/site/docker/runtimes /${DATA_PATH}/${APP_NAME}
# - 安装包及安装目录 
#   - ln -s /media/psf/Home/Documents/site/docker ${DATA_PATH}/install
# - docker 运行时配置目录(compose|dockerfile|conf)
#   - ln -s /media/psf/Home/Documents/site/docker/runtimes/docker ${DATA_PATH}/docker
# - docker 数据目录(镜像|容器)
#   - ln -s /media/psf/Home/Documents/site/docker/data ${DATA_PATH}/docker/data
# - sh 项目配置脚本
#   - ln -s /media/psf/Home/Documents/site/docker/runtimes/sh ${DATA_PATH}/sh
# - mysql
#   - ln -s /media/psf/Home/Documents/site/mysql ${DATA_PATH}/mysql
#   - ln -s /media/psf/Home/Documents/site/mariadb ${DATA_PATH}/mariadb
# - redis
#   - ln -s /media/psf/Home/Documents/site/docker/runtimes/redis ${DATA_PATH}/redis
# - nginx
#   - ln -s /media/psf/Home/Documents/site/docker/runtimes/nginx ${DATA_PATH}/nginx
# - php HTML_PATH: ${DATA_PATH}/site
#   - ln -s /media/psf/Home/Documents/site/docker/runtimes/php ${DATA_PATH}/php
#   - ln -s /media/psf/Home/Documents/site/insytone ${HTML_PATH}
# -----------------------------------------------------

# 安装配置文件
mkdir -p ${DATA_PATH}/${APP_NAME}
INSTALL_CONF="${DATA_PATH}/${APP_NAME}/install.conf"
# 镜像包构建文件
DOCKER_COMPOSE_PATH="${DATA_PATH}/docker/compose/${APP_NAME}"
DOCKER_COMPOSE_YML="${DOCKER_COMPOSE_PATH}/docker-compose.yml"

confirm="y"
if [ -f ${INSTALL_CONF} ]; then
  read -p "检查到配置文件，是否覆盖(y/N): " confirm
fi
if [ "${confirm}" == "y" ]; then
  # install.conf
  \cp -f ${CurrentDir}/install.conf ${INSTALL_CONF}
  sed -i -e "s|DATA_PATH=.*|DATA_PATH=${DATA_PATH}|g;" ${INSTALL_CONF}
  sed -i -e "s|APP_NAME=.*|APP_NAME=${APP_NAME}|g;" ${INSTALL_CONF}
  sed -i -e "s|AUTHOR=.*|AUTHOR=${AUTHOR}|g;" ${INSTALL_CONF}

  # 复制配置模板
  mkdir -p ${DATA_PATH}/docker/compose/${APP_NAME}
  \cp -r ${CurrentDir}/docker/. ${DATA_PATH}/docker/
  if [ "${APP_NAME}" != "lnmp" ]; then
    \cp ${CurrentDir}/docker/compose/lnmp/docker-compose.yml ${DOCKER_COMPOSE_YML}
  fi

  #  docker-compose.yml
  sed -i -e "s|DATA_PATH|${DATA_PATH}|g;" ${DOCKER_COMPOSE_YML}
  sed -i -e "s|APP_NAME|${APP_NAME}|g;" ${DOCKER_COMPOSE_YML}
  sed -i -e "s|AUTHOR|${AUTHOR}|g;" ${DOCKER_COMPOSE_YML}
fi

# 加载初始始配置文件及安装脚本
. ${INSTALL_CONF}
. sh/docker.sh
. sh/git.sh
. sh/cron.sh
. sh/nginx.sh
. sh/db.sh
. sh/redis.sh
. sh/php.sh

# 默认错误代码
ERROR=


# -----------------------------------------------------
# 初始化服务器环境
# -----------------------------------------------------

# 配置公钥
Check_Authorized

# 设置时区
Set_Timezone

# 切换国内源
Echo_Blue "切换国内源..."
if [ "${DISTRO}" = "Debian" ]; then
  Debian_Modify_Source
elif [ "${DISTRO}" = "Ubuntu" ]; then
  Ubuntu_Modify_Source
elif [ "${DISTRO}" = "RHEL" ]; then
  RHEL_Modify_Source
elif [ "${DISTRO}" = "CentOS" ]; then
  CentOS6_Modify_Source
fi

# 更新系统
Update_Dist

# 更新服务器配置: ip, uuid, ssh
Update_Server_Conf


# -----------------------------------------------------
# ${APP_NAME} 镜像选择及配置
# -----------------------------------------------------

Nginx_Image_Selection
DB_Image_Selection
Redis_Image_Selection
PHP_Image_Selection


# -----------------------------------------------------
# Docker
# -----------------------------------------------------

Install_Docker
Install_Docker_Compose

# 安装 Docker ${APP_NAME}
Echo_Blue "安装 Docker ${APP_NAME} 环境..."
# 切换 docker compose 配置目录
cd ${DOCKER_COMPOSE_PATH}

# 检查运行中的容器, 并尝试停止
Check_Docker_Containers
Echo_Blue "重新构建并启动 ${APP_NAME} 容器..."
# docker-compose up --build -d
docker compose up --build -d


# -----------------------------------------------------
# Git
# -----------------------------------------------------

Install_Git

# 安装项目
if [ -z "${OBJ_NAME}" ]; then
  read -p "项目名称(beehive): " OBJ_NAME
fi
if [ -z "${OBJ_NAME}" ]; then
  OBJ_NAME=beehive
fi

# 项目维护方式: 1) 本地拉取 pull | 2) 远程推送 push
Git_Origin_Update_Type_Selection
if [ "${GIT_ORIGIN_UPDATE_TYPE}" = "1" ]; then
  Init_Git ${OBJ_ORIGIN} ${OBJ_PATH}
elif [ "${GIT_ORIGIN_UPDATE_TYPE}" = "2" ]; then
  Init_Git_Bare ${OBJ_NAME}
  read -p "请等待开发者推送代码后继续完成安装..."
else
  read -p "按任意键继续..."
fi


# -----------------------------------------------------
# 项目部署
# -----------------------------------------------------

# 初始化项目数据库
DB_Init

# Echo_Blue "初始化 laravel8..."
# # 复制项目安装脚本, 映射: ${DATA_PATH}/${APP_NAME} => /data
# mkdir -p ${DATA_PATH}/${APP_NAME}/sh/laravel8
# \cp -r ${CurrentDir}/sh/laravel8/. ${DATA_PATH}/${APP_NAME}/sh/laravel8/
# # 进入 Docker PHP 容器安装项目, 精简的系统不支持 bash, 只安装了基本的 sh
# docker exec -it ${APP_NAME}-php-1 sh -c "/data/sh/laravel8/install.sh"

# Echo_Blue "添加定时任务: Laravel 命令行调度器..."
# # 每分钟调用一次 Laravel 的命令行调度器，添加到服务器中
# if ! crontab -l | grep -Eqi "/data/sh/laravel8/schedule.sh"; then
#   Add_Scheduled_Task "* * * * * docker exec ${APP_NAME}-php-1 sh -c \"/data/sh/laravel8/schedule.sh\""
# fi

Echo_Green "安装结束！"
echo

exit